import { isPlainObj } from '../core/isPlainObj'
import { each } from '../core/each'
import { cloneDeep } from './cloneDeep'
//19590
/**
 * 类似 extend，但会递归进行扩展
 * dependencies isPlainObj each cloneDeep
 * @param {*} destination 目标对象
 * @param  {...any} sources 源对象
 * @returns 目标对象
 */
export function extendDeep (obj, ...sources) {
  
    var i = 0;
    var ret = obj;
    var len = arguments.length;

    while (++i < len) {
      obj = arguments[i];

      if (isPlainObj(ret) && isPlainObj(obj)) {
        each(obj, function (val, key) {
          if (
            key === '__proto__' ||
            key === 'constructor' ||
            key === 'prototype'
          ) {
            return;
          }

          ret[key] = extendDeep(ret[key], obj[key]);
        });
      } else {
        ret = cloneDeep(obj);
      }
    }

    return ret;
}